package com.gict.antifraudback.controller.web.tags;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gict.antifraudback.entity.Tags;
import com.gict.antifraudback.exception.MyException;
import com.gict.antifraudback.result.ResultEnum;
import com.gict.antifraudback.result.ResultView;
import com.gict.antifraudback.service.TagsService;
import com.gict.antifraudback.vo.TagsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ThinkPad
 */
@RestController
@Slf4j
@RequestMapping("/wapi/tags")
public class TagsController {

    @Autowired
    private TagsService tagsService;

    @GetMapping("/getList")
    public ResultView getList(TagsVo tagsVo) {
        LambdaQueryWrapper<Tags> wrapper = Wrappers.lambdaQuery();
        wrapper.like(!ObjectUtils.isEmpty(tagsVo.getName()), Tags::getName, tagsVo.getName());
        return ResultView.ok(tagsService.page(new Page<>(tagsVo.getCurrent(), tagsVo.getPageSize()), wrapper));
    }

    @GetMapping("/getTree")
    public ResultView getTree(String name) {
        LambdaQueryWrapper<Tags> wrapper = Wrappers.lambdaQuery();
        wrapper.like(!ObjectUtils.isEmpty(name), Tags::getName, name);
        List<Tags> list = tagsService.list(wrapper);

        return ResultView.ok(list);
    }

    @GetMapping("/get")
    public ResultView get(@Valid @NotNull Integer id) {
        Tags tags = tagsService.getById(id);
        if (tags == null) {
            throw new MyException("标签不存在");
        }
        return ResultView.ok(tags);
    }

    @PostMapping("/delete")
    public ResultView delete(@RequestBody ArrayList<Integer> ids) {
        log.info("tagsVo:{}", ids);
        for (Integer id : ids) {
            tagsService.removeById(id);
        }
        return ResultView.okMsg("删除成功");
    }

    @PostMapping("/add")
    public ResultView add(@Valid @RequestBody Tags tags) {
        this.createBefore(tags);
        tagsService.save(tags);
        return ResultView.ok();
    }

    /**
     * 创建前检查
     */
    private void createBefore(Tags tags) {
        LambdaQueryWrapper<Tags> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Tags::getName, tags.getName());
        Tags exists = tagsService.getOne(wrapper);

        if (exists != null && !exists.getId().equals(tags.getId())) {
            throw new MyException(ResultEnum.CODE_2, "该标签已经存在");
        }
    }

    @PostMapping("/update")
    public ResultView update(@RequestBody @Valid Tags tags) {
        Tags update = tagsService.getById(tags.getId());
        if (update == null) {
            return ResultView.error(ResultEnum.CODE_2, "数据不存在！");
        }
        createBefore(tags);
        tagsService.updateById(tags);
        return ResultView.ok();
    }
}
